/***********************************************************************************************************************
 * 
 * > QuadroCore <
 * 
 * Copyright (C) 2012 by Chris Channing
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************/

#include "quadrocore.h"

static Font_t *defaultFontP = NULL;

static uint8_t defaultFontGlyphs[] =
{
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* "(space)" */
	0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x20,  /* "!" */
	0x50, 0x50, 0x50, 0x00, 0x00, 0x00, 0x00,  /* """ */
	0x50, 0x50, 0xf8, 0x50, 0xf8, 0x50, 0x50,  /* "#" */
	0x20, 0x78, 0xa0, 0x70, 0x28, 0xf0, 0x20,  /* "$" */
	0xc0, 0xc8, 0x10, 0x20, 0x40, 0x98, 0x18,  /* "%" */
	0x60, 0x90, 0xa0, 0x40, 0xa8, 0x90, 0x68,  /* "&" */
	0x60, 0x20, 0x40, 0x00, 0x00, 0x00, 0x00,  /* "'" */
	0x10, 0x20, 0x40, 0x40, 0x40, 0x20, 0x10,  /* "(" */
	0x40, 0x20, 0x10, 0x10, 0x10, 0x20, 0x40,  /* ")" */
	0x00, 0x50, 0x20, 0xf8, 0x20, 0x50, 0x00,  /* "*" */
	0x00, 0x20, 0x20, 0xf8, 0x20, 0x20, 0x00,  /* "+" */
	0x00, 0x00, 0x00, 0x00, 0x60, 0x20, 0x40,  /* "," */
	0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00,  /* "-" */
	0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x60,  /* "." */
	0x00, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00,  /* "/" */
	0x70, 0x88, 0x98, 0xa8, 0xc8, 0x88, 0x70,  /* "0" */
	0x20, 0x60, 0x20, 0x20, 0x20, 0x20, 0x70,  /* "1" */
	0x70, 0x88, 0x08, 0x10, 0x20, 0x40, 0xf8,  /* "2" */
	0xf8, 0x10, 0x20, 0x10, 0x08, 0x88, 0x70,  /* "3" */
	0x10, 0x30, 0x50, 0x90, 0xf8, 0x10, 0x10,  /* "4" */
	0xf8, 0x80, 0xf0, 0x08, 0x08, 0x88, 0x70,  /* "5" */
	0x30, 0x40, 0x80, 0xf0, 0x88, 0x88, 0x70,  /* "6" */
	0xf8, 0x08, 0x10, 0x20, 0x40, 0x40, 0x40,  /* "7" */
	0x70, 0x88, 0x88, 0x70, 0x88, 0x88, 0x70,  /* "8" */
	0x70, 0x88, 0x88, 0x78, 0x08, 0x10, 0x60,  /* "9" */
	0x00, 0x60, 0x60, 0x00, 0x60, 0x60, 0x00,  /* ":" */
	0x00, 0x60, 0x60, 0x00, 0x60, 0x20, 0x40,  /* ";" */
	0x08, 0x10, 0x20, 0x40, 0x20, 0x10, 0x08,  /* "<" */
	0x00, 0x00, 0xf8, 0x00, 0xf8, 0x00, 0x00,  /* "=" */
	0x80, 0x40, 0x20, 0x10, 0x20, 0x40, 0x80,  /* ">" */
	0x70, 0x88, 0x08, 0x10, 0x20, 0x00, 0x20,  /* "?" */
	0x70, 0x88, 0x08, 0x68, 0xa8, 0xa8, 0x70,  /* "@" */
	0x70, 0x88, 0x88, 0x88, 0xf8, 0x88, 0x88,  /* "A" */
	0xf0, 0x88, 0x88, 0xf0, 0x88, 0x88, 0xf0,  /* "B" */
	0x70, 0x88, 0x80, 0x80, 0x80, 0x88, 0x70,  /* "C" */
	0xe0, 0x90, 0x88, 0x88, 0x88, 0x90, 0xe0,  /* "D" */
	0xf8, 0x80, 0x80, 0xf0, 0x80, 0x80, 0xf8,  /* "E" */
	0xf8, 0x80, 0x80, 0xe0, 0x80, 0x80, 0x80,  /* "F" */
	0x70, 0x88, 0x80, 0x80, 0x98, 0x88, 0x70,  /* "G" */
	0x88, 0x88, 0x88, 0xf8, 0x88, 0x88, 0x88,  /* "H" */
	0x70, 0x20, 0x20, 0x20, 0x20, 0x20, 0x70,  /* "I" */
	0x38, 0x10, 0x10, 0x10, 0x10, 0x90, 0x60,  /* "J" */
	0x88, 0x90, 0xa0, 0xc0, 0xa0, 0x90, 0x88,  /* "K" */
	0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xf8,  /* "L" */
	0x88, 0xd8, 0xa8, 0x88, 0x88, 0x88, 0x88,  /* "M" */
	0x88, 0x88, 0xc8, 0xa8, 0x98, 0x88, 0x88,  /* "N" */
	0x70, 0x88, 0x88, 0x88, 0x88, 0x88, 0x70,  /* "O" */
	0xf0, 0x88, 0x88, 0xf0, 0x80, 0x80, 0x80,  /* "P" */
	0x70, 0x88, 0x88, 0x88, 0xa8, 0x90, 0x68,  /* "Q" */
	0xf0, 0x88, 0x88, 0xf0, 0xa0, 0x90, 0x88,  /* "R" */
	0x78, 0x80, 0x80, 0x70, 0x08, 0x08, 0xf0,  /* "S" */
	0xf8, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,  /* "T" */
	0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x70,  /* "U" */
	0x88, 0x88, 0x88, 0x88, 0x88, 0x50, 0x20,  /* "V" */
	0x88, 0x88, 0x88, 0xa8, 0xa8, 0xd8, 0x88,  /* "W" */
	0x88, 0x88, 0x50, 0x20, 0x50, 0x88, 0x88,  /* "X" */
	0x88, 0x88, 0x50, 0x20, 0x20, 0x20, 0x20,  /* "Y" */
	0xf8, 0x08, 0x10, 0x20, 0x40, 0x80, 0xf8,  /* "Z" */
	0x38, 0x20, 0x20, 0x20, 0x20, 0x20, 0x38,  /* "[" */
	0x00, 0x80, 0x40, 0x20, 0x10, 0x08, 0x00,  /* "" */
	0xe0, 0x20, 0x20, 0x20, 0x20, 0x20, 0xe0,  /* "]" */
	0x20, 0x50, 0x88, 0x00, 0x00, 0x00, 0x00,  /* "^" */
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8,  /* "_" */
	0x40, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00,  /* "`" */
	0x00, 0x00, 0x70, 0x08, 0x78, 0x88, 0x78,  /* "a" */
	0x80, 0x80, 0xb0, 0xc8, 0x88, 0x88, 0xf0,  /* "b" */
	0x00, 0x00, 0x70, 0x80, 0x80, 0x88, 0x70,  /* "c" */
	0x08, 0x08, 0x68, 0x98, 0x88, 0x88, 0x78,  /* "d" */
	0x00, 0x00, 0x70, 0x88, 0xf8, 0x80, 0x70,  /* "e" */
	0x30, 0x48, 0x40, 0xe0, 0x40, 0x40, 0x40,  /* "f" */
	0x00, 0x00, 0x78, 0x88, 0x78, 0x08, 0x30,  /* "g" */
	0x80, 0x80, 0xb0, 0xc8, 0x88, 0x88, 0x88,  /* "h" */
	0x20, 0x00, 0x60, 0x20, 0x20, 0x20, 0x70,  /* "i" */
	0x10, 0x00, 0x30, 0x10, 0x10, 0x90, 0x60,  /* "j" */
	0x40, 0x40, 0x48, 0x50, 0x60, 0x50, 0x48,  /* "k" */
	0x60, 0x20, 0x20, 0x20, 0x20, 0x20, 0x70,  /* "l" */
	0x00, 0x00, 0xd0, 0xa8, 0xa8, 0x88, 0x88,  /* "m" */
	0x00, 0x00, 0xb0, 0xc8, 0x88, 0x88, 0x88,  /* "n" */
	0x00, 0x00, 0x70, 0x88, 0x88, 0x88, 0x70,  /* "o" */
	0x00, 0x00, 0xf0, 0x88, 0xf0, 0x80, 0x80,  /* "p" */
	0x00, 0x00, 0x68, 0x98, 0x78, 0x08, 0x08,  /* "q" */
	0x00, 0x00, 0xb0, 0xc8, 0x80, 0x80, 0x80,  /* "r" */
	0x00, 0x00, 0x70, 0x80, 0x70, 0x08, 0xf0,  /* "s" */
	0x40, 0x40, 0xe0, 0x40, 0x40, 0x48, 0x30,  /* "t" */
	0x00, 0x00, 0x88, 0x88, 0x88, 0x98, 0x68,  /* "u" */
	0x00, 0x00, 0x88, 0x88, 0x88, 0x50, 0x20,  /* "v" */
	0x00, 0x00, 0x88, 0x88, 0xa8, 0xa8, 0x50,  /* "w" */
	0x00, 0x00, 0x88, 0x50, 0x20, 0x50, 0x88,  /* "x" */
	0x00, 0x00, 0x88, 0x88, 0x78, 0x08, 0x70,  /* "y" */
	0x00, 0x00, 0xf8, 0x10, 0x20, 0x40, 0xf8,  /* "z" */
	0x10, 0x20, 0x20, 0x40, 0x20, 0x20, 0x10,  /* "{" */
	0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,  /* "|" */
	0x40, 0x20, 0x20, 0x10, 0x20, 0x20, 0x40,  /* "}" */
	0x00, 0x20, 0x10, 0xf8, 0x10, 0x20, 0x00,  /* "->" */
	0x00, 0x20, 0x40, 0xf8, 0x40, 0x20, 0x00   /* "<-" */
};

Font_t* AllocDefaultFont(void)
{
	Font_t *defaultFontP = calloc(1, sizeof(Font_t));
	
	if (defaultFontP)
	{
		defaultFontP->height = 7;
		defaultFontP->width = 5;
		defaultFontP->dataP = defaultFontGlyphs;
		defaultFontP->getCharacterGlyphFunc = &DefaultFontGetCharacterGlyph;
	}
	
	return defaultFontP;
}

Font_t* GetDefaultFont(void)
{
	if (! defaultFontP)
	{
		defaultFontP = AllocDefaultFont();
	}
	
	return defaultFontP;
}

uint8_t* DefaultFontGetCharacterGlyph(Font_t *fontP, char character)
{
	if (! fontP) return NULL;

	return (fontP->dataP + ((character - 32) * 7));
}